#include "../../include/fileSystem/fileDescriptor.h"
#include "../../include/fileSystem/inode.h"
#include "../../include/process.h"
#include "../../include/kc.h"
#include "../../include/defs.h"

extern process_t processes[];	//lista de procesos
extern int currProc;			//proceso actual

int
setFileDes(inodeTableEntry * inode, int filePos, int flags) {
	int i;
	for (i = 0; i < FD_PER_PROC; i++) {
		if (processes[currProc].fd[i].inode == NULL) {
			processes[currProc].fd[i].inode = inode;
			processes[currProc].fd[i].filePos = filePos;
			processes[currProc].fd[i].flags = flags;
			return i;
		}
	}

	return -1;
}

int
updateFileDes(int fd, int filePos) {
	if (processes[currProc].fd[fd].inode == NULL)
		return -1;

	processes[currProc].fd[fd].filePos = filePos;

	return 1;
}

void
freeFileDes(int fd) {
	if (processes[currProc].fd[fd].inode == NULL)
		return;

	putInode(processes[currProc].fd[fd].inode);
	processes[currProc].fd[fd].inode = NULL;
}

int
getFilePos(int fd) {
	if (processes[currProc].fd[fd].inode == NULL)
		return -1;

	return processes[currProc].fd[fd].filePos;
}

short int
getFileFlags(int fd) {
	if (processes[currProc].fd[fd].inode == NULL)
		return -1;

	return processes[currProc].fd[fd].flags;
}

inodeTableEntry *
getFdInode(int fd) {
	return processes[currProc].fd[fd].inode;
}
